<?php

/**
 * This is the model class for table "{{transaction}}".
 *
 * The followings are the available columns in table '{{transaction}}':
 * @property integer $idtransaction
 * @property string $transactionmastervoucherdate
 * @property string $transactionmasternarration
 * @property integer $idvouchertype
 * @property string $transactiondetails
 *
 * The followings are the available model relations:
 * @property Vouchertype $idvouchertype0
 */
class Transaction extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Transaction the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{transaction}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('transactionmastervoucherdate, transactionmasternarration, idvouchertype', 'required'),
            array('idvouchertype', 'numerical', 'integerOnly' => true),
            array('transactionmasternarration', 'length', 'max' => 255),
            array('transactiondetails', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idtransaction, transactionmastervoucherdate, transactionmasternarration, idvouchertype, transactiondetails', 'safe', 'on' => 'search'),
            array('yii_vouchertype', 'autocompleteValidator', 'model' => 'Vouchertype'),
        );
    }

    public function autocompleteValidator($attribute, $params) {
        $match = addcslashes($_REQUEST[$attribute], '%_');
        if (!new $params['model']->exists(new CDbCriteria(array(
                            'condition' => "$attribute = :$attribute",
                            'params' => array(":$attribute" => "$match")
                        )))) {
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
        }
    }

    public function beforeSave() {
        if (isset($_REQUEST['yii_vouchertype'])) {
            $match = addcslashes($_REQUEST['yii_vouchertype'], '%_'); // escape LIKE's special characters
            $criteria = new CDbCriteria(array(
                        'condition' => 'yii_vouchertype = :yii_vouchertype',
                        'params' => array(':yii_vouchertype' => "$match")
                    ));
            $model = Vouchertype::model()->find($criteria);
            $this->idvouchertype = $model->idvouchertype;
        }
        return parent::beforeSave();
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'idvouchertype0' => array(self::BELONGS_TO, 'Vouchertype', 'idvouchertype'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idtransaction' => Yii::t('application', 'Idtransaction'),
            'transactionmastervoucherdate' => Yii::t('application', 'Transactionmastervoucherdate'),
            'transactionmasternarration' => Yii::t('application', 'Transactionmasternarration'),
            'idvouchertype' => Yii::t('application', 'Idvouchertype'),
            'transactiondetails' => Yii::t('application', 'Transactiondetails'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('idtransaction', $this->idtransaction);
        $criteria->compare('transactionmastervoucherdate', $this->transactionmastervoucherdate, true);
        $criteria->compare('transactionmasternarration', $this->transactionmasternarration, true);
        $criteria->compare('idvouchertype', $this->idvouchertype);
        $criteria->compare('transactiondetails', $this->transactiondetails, true);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

    public function transactiondetailsDataProvider($idtransactionmaster) {
    Yii::app()->clientscript->scriptMap['jquery.js'] = false;
    Yii::app()->clientscript->scriptMap['jquery-ui.min.js'] = false;
//    Yii::app()->clientscript->scriptMap['jquery.yiigridview.js'] = false;

//    $cs=Yii::app()->getClientScript();
//    $cs->registerCoreScript('jquery');
//    $cs->registerCoreScript('jquery-ui');
//    $cs->registerCoreScript('yiigridview');
        
        
        $rawData = Yii::app()->session['rawdata'];
        if(!isset($rawData)){
            $rawData = array();
//            $rawData[] = array('id' => 0, 'openingbalancecrdr' => 'CR', 'acledger'=>'');
            if($idtransactionmaster > 0){
                $transactiondetailss = Transactiondetails::model()->findAll('idtransactionmaster=:idtransactionmaster', array(':idtransactionmaster'=>$idtransactionmaster));
                foreach($transactiondetailss as $transactiondetails){
                    $rawData[count($rawData)] = array('id' => count($rawData), 'openingbalancecrdr' => $transactiondetails->transactioncrdr, 'acledger' => $transactiondetails->idacledger0->acledgername, 'transactionamount' => $transactiondetails->transactionamount, 'credit' => $transactiondetails->transactioncrdr == 'CR' ? $transactiondetails->transactionamount : '', 'debit' => $transactiondetails->transactioncrdr == 'DR' ? $transactiondetails->transactionamount : '');
                    syslog(LOG_WARNING, print_r($rawData, true));
                }
            }
            Yii::app()->session['rawdata'] = $rawData;            
        }
//        $cnt = isset(Yii::app()->session['rowcount']) ? Yii::app()->session['rowcount'] : 1;
//        for ($i = 0; $i <= Yii::app()->session['rowcount'] ; $i++) {
//            $rawData[$i]['id'] = $i . ' - ' . $this->transactiondetails;
//        }
//        
//        if(isset($_POST['transactionmasternarration']))
//        $rawData[0]['id'] = $rawData[0]['id'] . ' - ' . $_POST['transactionmasternarration'];
//        foreach($this->transactiondetails as $key=>$value){
//            $rawData[$key]['id'] = $i;            
//        }

        $dataProvider = new CArrayDataProvider($rawData, array(
                    'id' => 'transactiondetailsDataProvider',
                ));
        return $dataProvider;
    }

}